/*
*  Copyright 2019-2023 oyun
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/package com.oyun.modules.document.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.document.domain.Catalogue;
import com.oyun.modules.document.domain.vo.CatalogueQueryCriteria;
import com.oyun.modules.document.mapper.CatalogueMapper;
import com.oyun.modules.document.service.CatalogueService;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author oyun
* @date 2024-05-23
**/
@Service
@RequiredArgsConstructor
public class CatalogueServiceImpl extends ServiceImpl<CatalogueMapper, Catalogue> implements CatalogueService {

    private final CatalogueMapper catalogueMapper;

    @Override
    public PageResult<Catalogue> queryAll(CatalogueQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(catalogueMapper.findAll(criteria, page));
    }

    @Override
    public List<Catalogue> queryAll(CatalogueQueryCriteria criteria){
        return catalogueMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Catalogue resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Catalogue resources) {
        Catalogue catalogue = getById(resources.getId());
        catalogue.copy(resources);
        saveOrUpdate(catalogue);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Catalogue> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Catalogue catalogue : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" docId",  catalogue.getDocId());
            map.put("封面", catalogue.getCoverB());
            map.put(" coverE",  catalogue.getCoverE());
            map.put("扇页", catalogue.getFLeafB());
            map.put(" fLeafE",  catalogue.getFLeafE());
            map.put("版权页", catalogue.getCPageB());
            map.put(" cPageE",  catalogue.getCPageE());
            map.put("总目", catalogue.getGHeadingB());
            map.put(" gHeadingE",  catalogue.getGHeadingE());
            map.put("出版说明", catalogue.getPNoteB());
            map.put(" pNoteE",  catalogue.getPNoteE());
            map.put("前言", catalogue.getIntroductionB());
            map.put(" introductionE",  catalogue.getIntroductionE());
            map.put("序", catalogue.getSequenceB());
            map.put(" sequenceE",  catalogue.getSequenceE());
            map.put("凡例", catalogue.getExampleB());
            map.put(" exampleE",  catalogue.getExampleE());
            map.put("音序索引", catalogue.getPIndexB());
            map.put(" pIndexE",  catalogue.getPIndexE());
            map.put("正文", catalogue.getMBadyB());
            map.put(" mBodyE",  catalogue.getMBodyE());
            map.put("附录", catalogue.getAppendixB());
            map.put(" appendixE",  catalogue.getAppendixE());
            map.put("封底", catalogue.getBackB());
            map.put(" backE",  catalogue.getBackE());
            map.put(" createBy",  catalogue.getCreateBy());
            map.put(" createTime",  catalogue.getCreateTime());
            map.put(" updateBy",  catalogue.getUpdateBy());
            map.put(" updateTime",  catalogue.getUpdateTime());
            map.put(" remark",  catalogue.getRemark());
            map.put(" status",  catalogue.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
